from pydantic import BaseModel, field_validator
from typing import Optional,List, Dict
from datetime import date

class MedicineItem(BaseModel):
    ID: int
    dose: int
    frequency: str

    @field_validator("dose")
    @classmethod
    def validate_dose(cls, field):
        if field<=0:
            raise ValueError("药品数量必须大于0")
        return field



class PrescriptionBase(BaseModel):
    doctor_ID: int
    patient_ID: int
    pre_date: date
    medicines: List[MedicineItem]
    is_done: str


class PrescriptionCreate(PrescriptionBase):
    pass

class PrescriptionUpdate(BaseModel):
    doctor_ID: Optional[int] = None
    patient_ID: Optional[int] = None
    pre_date: Optional[date] = None
    medicines: Optional[List[MedicineItem]] = None
    is_done: str


class PrescriptionResponse(PrescriptionBase):
    ID: int

    class Config:
        from_attributes = True


class MedicineItemWithName(MedicineItem):
    medicine_name: str

class PrescriptionWithPaymentStatusResponse(BaseModel):
    ID: int
    doctor_ID: int
    patient_ID: int
    pre_date: date
    medicines: List[MedicineItemWithName]
    is_done: str

    is_paid: bool
    payment_id: Optional[int]

    class Config:
        from_attributes = True


# 药剂师所用模型
class MedicineItemWithName(BaseModel):
    ID: int
    dose: int
    frequency: str
    medicine_name: str

class DoctorInfo(BaseModel):
    ID: int
    name: str
    age: Optional[int]
    phone_number: Optional[str]
    gender: Optional[str]
    title: Optional[str]
    position: Optional[str]
    department: Optional[str]
    specialty: Optional[str]
    image_url: Optional[str]

class PatientInfo(BaseModel):
    ID: int
    name: str
    gender: Optional[str]
    age: Optional[int]
    phone_number: Optional[str]

class PrescriptionWithPaymentStatusResponse(BaseModel):
    ID: int
    doctor_ID: int
    patient_ID: int
    pre_date: date
    medicines: List[MedicineItemWithName]
    is_done: str

    is_paid: bool
    payment_id: Optional[int]

    doctor: Optional[DoctorInfo]=None
    patient: Optional[PatientInfo]=None

    class Config:
        orm_mode = True